# -*- coding: utf-8 -*-

import os


CC_Method = raw_input('input Cost Compute method:')
CA_Method = raw_input('input Cost Aggregate method:')
PP_Method = raw_input('input PostProcess method(NULL means no PostProcess):')         # it is optional
C_Alpha = raw_input('input parameter Alpha:')
maxDis = raw_input('input maxmun disparity:')
disSc = raw_input('input disparity scale:')
testImg = raw_input('input test img("all or FourImg or testQ or traingQ"):')      # to confirm circulate:
dispsuffix = raw_input('input suffix of disparity:')

if 0 == os.path.isdir('img/testQ') or 0 == os.path.isdir('img/trainingQ' or 0 == os.path.isdir('FourImg')):
    print 'image directory testQ or traingQ OR FourImg is not found!'

if testImg == 'all' :
    imgdir = 'img'
elif testImg == 'FourImg':
    imgdir = 'img/FourImg'
elif testImg == 'trainingQ':
    imgdir = 'img/trainingQ'
elif testImg == 'testQ':
    imgdir = 'img/testQ'
elif os.path.exists('img/FourImg/' + testImg ):
    imgdir = 'img/FourImg/' + testImg
elif os.path.exists('img/testQ/' + testImg ):
    imgdir = 'img/testQ/' + testImg
elif os.path.exists('img/trainingQ/' + testImg ):
    imgdir = 'img/trainingQ/' + testImg
imgcount = 0
if testImg == 'all':
    imgset = os.listdir(imgdir)
    for eachset in imgset:
        imglist = os.listdir(imgdir + '/' + eachset)
        for eachimg in imglist:
            img = imgdir + '/' + eachset + '/' + eachimg
            if 0 == os.path.exists(img + '/' + 'im0.png') or 0 == os.path.exists(img + '/' + 'im1.png'):
                print 'im0 or im1 not found'
                break
            lDis = img + '/' + 'disp0' + dispsuffix + '.png'
            limg = img + '/im0.png'
            rimg = img + '/im1.png'
            if PP_Method == 'NULL':
                myCommand = 'SSCA.exe' + CC_Method + CA_Method + PP_Method + C_Alpha + limg + \
                            rimg + lDis + maxDis + disSc
            else:
                rDis = img + '/' + 'disp1' + dispsuffix + '.png'
                myCommand = 'SSCA.exe' + ' ' + CC_Method + ' ' + CA_Method + ' ' + PP_Method + ' ' + C_Alpha + ' ' + \
                            limg + ' ' + rimg + ' ' + lDis + ' ' + rDis + ' ' + maxDis + ' ' + disSc
            try:
                os.system(myCommand)
            except Exception:
                continue
            imgcount += 1
            print imgcount
elif testImg == 'FourImg' or testImg == 'testQ' or testImg == 'trainingQ':
    imglist = os.listdir(imgdir)
    for eachimg in imglist :
        img = imgdir + '/' + eachimg
        if 0 == os.path.exists(img + '/' + 'im0.png') or 0 == os.path.exists(img + '/' + 'im1.png'):
            print 'im0 or im1 not found'
            break
        lDis = img + '/' + 'disp0' + dispsuffix + '.png'
        limg = img + '/im0.png'
        rimg = img + '/im1.png'
        if PP_Method == 'NULL':
            myCommand = 'SSCA.exe' + CC_Method + CA_Method + PP_Method + C_Alpha + limg + \
                        rimg + lDis + maxDis + disSc
        else:
            rDis = img + '/' + 'disp1' + dispsuffix + '.png'
            myCommand = 'SSCA.exe' + ' ' + CC_Method + ' ' + CA_Method + ' ' + PP_Method + ' ' + C_Alpha + ' ' + \
                        limg + ' ' + rimg + ' ' + lDis + ' ' + rDis + ' ' + maxDis + ' ' + disSc
        os.system(myCommand)
        imgcount += 1
        print imgcount
else:
    img = imgdir
    if 0 == os.path.exists(img + '/' + 'im0.png') or 0 == os.path.exists(img + '/' + 'im1.png'):
        print 'im0 or im1 not found'
    lDis = img + '/' + 'disp0' + dispsuffix + '.png'
    limg = img + '/im0.png'
    rimg = img + '/im1.png'
    if PP_Method == 'NULL':
        myCommand = 'SSCA.exe' + CC_Method + CA_Method + PP_Method + C_Alpha + limg + \
                    rimg + lDis + maxDis + disSc
    else:
        rDis = img + '/' + 'disp1' + dispsuffix + '.png'
        myCommand = 'SSCA.exe' + ' ' + CC_Method + ' ' + CA_Method + ' ' + PP_Method + ' ' + C_Alpha + ' ' + \
                    limg + ' ' + rimg + ' ' + lDis + ' ' + rDis + ' ' + maxDis + ' ' + disSc
    os.system(myCommand)
print 'Run Alogrithm On Specified Dataset Done!'
